#!/usr/bin/env python
# -*- coding: utf-8 -*-





from subprocess import call
import os, inspect, sys, shutil

#def which(program):
#
#    def is_exe(fpath):
#        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
#    fpath, fname = os.path.split(program)
#    if fpath:
#        if is_exe(program):
#            return program
#    else:
#        for path in os.environ["PATH"].split(os.pathsep):
#            exe_file = os.path.join(path, program)
#            if is_exe(exe_file):
#                return exe_file
#
#    return None

#cd C:\Software\DESLABV0.0.2WIN\DESLAB002WIN
#
#python setup.py sdist --format=zip
#
#cd C:\Software\DESLABV0.0.2WIN\DESLAB002WIN\dist
#unzip -o DESlab-0.0.2.zip
#cd DESlab-0.0.2
#python setup.py install
#cd C:\Software\DESLABV0.0.2WIN\DESLAB002WIN

from deslab.version import __version__

def make_package(install = False):
    """This function builds a complete relase package for windows"""
    # determining current path
    curr_path = inspect.currentframe().f_code.co_filename
    dir_path  = os.path.dirname(curr_path)   
    command ='python setup.py sdist --format=zip'
    os_resp = call(command, shell=True, cwd=dir_path)
    dist_path = os.path.join(dir_path,'dist')
    command ='unzip -o DESlab-'+__version__+'.zip'
    os_resp = call(command, shell=True, cwd=dist_path)
    pack_path = os.path.join(dir_path,'dist','DESlab-'+__version__)  
    if install:  
        command ='python setup.py install'
        os_resp = call(command, shell=True, cwd=pack_path)        
    return

def make_wininstaller(install=False):
    curr_path = inspect.currentframe().f_code.co_filename
    dir_path  = os.path.dirname(curr_path)   
    command = 'python setup.py build bdist_wininst  --bitmap deslab_logo.bmp --install-script win_script.py '
    os_resp = call(command, shell=True, cwd=dir_path)
    dist_path = os.path.join(dir_path,'dist','DESlab-'+__version__+'.win32.exe')
    pack_path = os.path.join(dir_path,'dist','DESlab-'+__version__, 'DESlab-'+__version__+'.win32.exe') 
    shutil.move(dist_path, pack_path)
    if install: 
        os.startfile(pack_path)   
    zip_path = os.path.join(dir_path,'dist','DESlab-'+__version__+'.zip')
    ver_path = os.path.join(dir_path,'dist','DESlab-'+__version__)
    os.remove(zip_path) 
    #command ='zip -o DESlab-'+__version__
    #os_resp = call(command, shell=True, cwd=ver_path)       
    return
make_package(install = False)
make_wininstaller(False) 



#make_wininstaller() 

#    curr_path = inspect.currentframe().f_code.co_filename
#    dir_path  = os.path.dirname(curr_path)
#    d2t_path = os.path.join(dir_path,'packages','dot2tex-2.8.7')
#    command ='python setup.py install'
#    os_resp = call(command, shell=True, cwd=d2t_path)
#    return
#
#def install_pyparsing():  
#    curr_path = inspect.currentframe().f_code.co_filename
#    dir_path  = os.path.dirname(curr_path)
#    d2t_path = os.path.join(dir_path,'packages','pyparsing-1.5.6')
#    command ='python setup.py install'
#    os_resp = call(command, shell=True, cwd=d2t_path)
#    return
#
#def install_pydot():  
#    curr_path = inspect.currentframe().f_code.co_filename
#    dir_path  = os.path.dirname(curr_path)
#    d2t_path = os.path.join(dir_path,'packages','pydot-1.0.28')
#    command ='python setup.py install'
#    os_resp = call(command, shell=True, cwd=d2t_path)
#    return
#
#def test_networkx():
#    """ Testing networkx dependence """
#    try:
#        import networkx
#        from networkx.version import version_info 
#        curr_version = version_info[1:3]
#        if  curr_version >= (1,5):
#            ver = str(curr_version[0])+'.'+str(curr_version[1])    
#            print >> sys.stderr,"version %s of Networkx found, and it is valid"%ver
#        else:
#            install_networkx()
#    except ImportError:
#        install_networkx()
#        print >> sys.stderr," I am installing Networkx v1.7"
#    return
#
#
#
#def test_dot2tex():
#    """ Testing dot2tex dependence """
#    try:
#        import dot2tex
#        from dot2tex.dot2tex import  __version__ 
#        curr_ver = __version__.split('.')
#        curr_ver = (int(curr_ver[0]), int(curr_ver[1]))
#        if  curr_ver >= (2,5):       
#            print >> sys.stderr, "version %s of dot2tex found, and it is valid"%__version__
#        else:            
#            install_dot2tex()  
#            print >> sys.stderr,"I am installing dot2tex v2.8"      
#    except ImportError:
#        install_dot2tex()
#    return
#
#
#def test_pyparsing():
#    """ Testing pyparsing dependence """
#    try:
#        import pyparsing
#        from pyparsing import  __version__ 
#        curr_ver = __version__.split('.')
#        curr_ver = (int(curr_ver[0]), int(curr_ver[1]))
#        if  curr_ver >= (1,0):       
#            print >> sys.stderr, "version %s of pyparsing found, and it is valid."%__version__
#        else:
#            install_pyparsing()        
#            print >> sys.stderr,"I am installing pyparsing 1.5.6"
#    except ImportError:
#        install_pyparsing()
#    return
#
#
#
#
#def test_pydot():
#    """ Testing pydot dependence """
#    try:
#        import pydot
#        from pydot import  __version__ 
#        curr_ver = __version__.split('.')
#        curr_ver = (int(curr_ver[0]), int(curr_ver[1]))
#        if  curr_ver >= (1,0):       
#            print >> sys.stderr, "version %s of pydot found, and it is valid"%__version__
#        else:
#            install_pydot()      
#            print >> sys.stderr,"I am installing pydot v1.0.28"  
#    except ImportError:
#        install_pydot()
#    return
#
#def test_graphviz():
#    """ Testing graphviz dependence """
#    if  which('dot.exe'):
#        #print >> sys.stderr,  which('dot.exe')
#        print >> sys.stderr, '- Graphviz is correctly installed'
#    else:
#        print '- latex was not yet installed. Please install it to get graphic functions' 
#        #sys.exit(-1)
#    return
#    
#
#def test_pdflatex():            
#    """ 
#    Testing latex dependence
#    """
#    if  which('pdflatex.exe'):
#        print >> sys.stderr, '- pdflatex is correctly installed'
#    else:
#        print >> sys.stderr, '- latex was not yet installed. Please install it to get graphic functions' 
#        #sys.exit(-1)
#    return
#
#
#
#
#
#print >> sys.stderr,""
#print >> sys.stderr,"I am revising executable programs:"
#print >> sys.stderr,"----------------------------------------------"
#print >> sys.stderr,""
#print >> sys.stderr,""
#test_graphviz()
#test_pdflatex()
#print >> sys.stderr,""
#print >> sys.stderr,"I am revising and installing required python packages:"
#print >> sys.stderr,"-----------------------------------------------------------------"
#print >> sys.stderr,""
#test_networkx()
#test_pyparsing()
#test_pydot()
#test_dot2tex()
#print >> sys.stderr,""
#print >> sys.stderr, "I just completed  DESlab installation."
#print >> sys.stderr, "Thanks for your interest in this program."
#





